class FavoriteController < ApplicationController
  layout "favorite"
  before_filter :authorize, :except => [:show, :user]
  
  # Skip the privacy check now. Just show the personal message page.
  @paged = false
  
  def index
    if session[:user_name].nil?
      redirect_to(:controller => :home, :action => "index")
    else
      users = User.find(:all, :conditions => ["name='#{session[:user_name]}'"])
      @user = users[0]
      @page_title = "My Favorites_Microblog"
      get_paged_favorites
      @message_count = get_message_count
    end
  end
  
  def user
    users = User.find(:all, :conditions => ["id='#{params[:id]}'"])
    @user = users[0]
    @message_count = get_message_count
    # Judge if the user is himself. if so, redirect to home.
    if @user.name == session[:user_name]
      redirect_to(:controller => :favorite, :action => "index")
    else
      get_paged_favorites
      @page_title = "#{@user.display_name}'s Favorites_Microblog"
      render(:action => "index")
    end
  end
  
  def show
    # Put the controller in the wrong place, so use some dirty way to solve it.
    # This should be resolved in later revision.
    @narrowlook = true
    @favorite = Favorite.find(params[:id])
    @user = User.find(@favorite.user_id)
    @page_title = "#{@user.display_name}'s Favorite_Microblog"
  end
  
  def delete
    fav = Favorite.find(params[:id])
    user = User.find(fav.user_id)
    unless session[:user_name] == user.name
      # If unauthorized user attempt to delete message
      # Simply redirect to home page. No error message displayed.
      redirect_to(:controller => :home, :action => "index")
    else
      begin
        fav.destroy
        flash[:notice] = "Favorite deleted successfully."
      rescue Exception => e
        flash[:notice] = e.message
      end
      redirect_to(:controller => :favorite, :action => "index")
    end
  end
  
  def add
    # get current user id
    user = get_current_user(session[:user_name])
    # get message content
    message = Message.find(params[:id])
    message_poster = User.find(message.user_id)
    # initiate favorite
    favorite = Favorite.new
    favorite.user_id = user.id
    favorite.title = message_poster.display_name
    # because of the design flaw, the favorite table cannot store
    # the message properly if it is shared here.
    # salve this problem in next version.
    # temperarily ignore the url portion
    # I may add an extra column to indentify the source.
    favorite.url = ""
    favorite.description = message.content
    favorite.favorite_at = Time.now
    if favorite.save
      flash[:notice] = "Successfully favorited."
    else
      flash[:notice] = "Something wrong while favorite the message."
    end
    uri = session[:ori_uri]
    session[:ori_uri] = nil
    redirect_to( uri || {:controller => :message, :action => "index"} )
  end
  
  
  private
  
  def get_favorites_count(id)
    Favorite.find(:all, :conditions => ["user_id='#{id}'"]).length
  end
  
  def get_message_count
    Message.find(:all, :conditions => ["user_id='#{@user.id}'"]).length
  end
  
  def get_paged_favorites
    @favorites_count = get_favorites_count(@user.id)
    @page_count = (@favorites_count / 20.0).ceil
    if @favorites_count > 20
      @paged = true
    end
    @page = params[:page].nil? ? 1 : params[:page].to_i
    @favorites = Favorite.find(:all, :conditions => ["user_id='#{@user.id}'"],
      :order => "favorite_at DESC", :limit => 20, :offset => ((@page - 1) * 20))
  end
  
  def get_current_user(name)
    users = User.find(:all, :conditions => ["name='#{name}'"])
    users[0]
  end
  
end
